<template>
  <div>
    <el-card class="top"><el-header>红包详情</el-header></el-card>

    <!-- 表格展示 -->
    <el-card>
      <el-input placeholder="红包名称" prefix-icon="el-icon-search" style="width: 200px" v-model="name">
      </el-input>
      <el-button type="warning" @click="reload" plain>查询</el-button>
      <el-table :data="redList" style="width: 100%" :sort-by.sync="sortKey" :sort-orders="['ascending', 'descending']">
        <el-table-column type="index" label="编号" width="60" align="center">
        </el-table-column>
        <el-table-column prop="name" label="红包名称" width="150" align="center">
        </el-table-column>
        <el-table-column prop="money" label="红包金额（元）" width="120" align="center" sortable>
        </el-table-column>
        <el-table-column prop="num" label="总数" width="100" align="center" sortable>
        </el-table-column>
        <el-table-column prop="starttime" label="开始时间" width="200" align="center" sortable>
        </el-table-column>
        <el-table-column prop="endtime" label="结束时间" width="200" align="center" sortable>
        </el-table-column>
        <el-table-column prop="state" label="状态" width="150" align="center" sortable>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <!-- v-if="canEditAndDelete"  v-else-->
            <el-button size="mini" type="danger" @click="pay(scope.row)">抢红包</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--    分页-->
    <el-row>
      <el-col style="text-align:right">
        <el-pagination :current-page="currentPage" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :page-size="pageSize" :page-sizes="[5, 10, 15]" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
  </div>

</template>

<script>
import { del } from 'vue';

export default {
  data() {
    return {
      uid: null,
      rid: null,
      user: {},
      name: "",
      redList: [],
      timevalue: [new Date(), new Date()],
      formLabelWidth: '120px',
      //配置分页
      currentPage: 1,
      pageSize: 5,
      total: 0,
      sortKey: ''
    };
  },

  mounted() {
    this.reload();
    if (localStorage.getItem("user")) {
      this.user = JSON.parse(localStorage.getItem("user"))
      console.log(JSON.stringify(this.user.uid));
      let uid = this.user.uid;

    }
  },

  methods: {

    // 抢红包
    pay(row) {
      console.log(row);
      let rid = row.id;
      console.log(rid);
      console.log(row.num);
      if (row.num == 0) {
        this.$message({
          message: "红包已抢完",
          type: 'error'
        });
      } else {
        this.$axios.post("/killredEnvelope/pay/" + rid).then((res) => {
          console.log(res.data);
          if (res.data.code == 200) {
            this.$message({
              message: "抢红包成功",
              type: 'success'
            });
          } else {
            this.$message.error(res.data.message);
          }
        });
        this.reload();
      }
    },

    reload() {

      this.$axios.get("/killredEnvelope/getKillRedEnvelope?currentPage=" + this.currentPage + "&pageSize=" + this.pageSize + "&name=" + this.name).then((res) => {
        console.log(res.data.data);
        this.redList = res.data.data.list;
        this.total = res.data.data.total;
      });
    },

    handleChange(value) {
      console.log(value);
    },

    //显示查询出的总条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.reload();
    },

    //点击新页查询的数据
    handleCurrentChange(val) {
      this.currentPage = val;
      this.reload();
    },

    changeTime() {
      this.addform.starttime = this.timevalue[0]
      this.addform.endtime = this.timevalue[1]
    }
  },
};
</script>

<style scoped>
.el-header {
  text-align: center;
  line-height: 30px;
  size: 120px;
}

.top {
  background-color: #D3D3D3;
  height: 60px;
}

.el-card {
  margin-top: 10px;
  margin-left: 30px;
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>